package com.vertx.user.store;

import com.vertx.core.annotation.data.Query;
import com.vertx.core.common.ResultFormat;
import com.vertx.core.common.StatusCodeMsg;
import com.vertx.data.entity.SqlPropertyValue;
import com.vertx.data.mysql.AbstractSQL;
import com.vertx.data.utils.DbUtils;
import com.vertx.user.model.User;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;

import java.util.List;

/**
 * 描述:
 * 创建人:hzw
 * 创建时间: 2018/5/2 18:09
 **/
@Query
public class UserServiceImpl extends AbstractSQL<User> implements UserService {

    @Override
    protected String tableName() {
        return "t_user";
    }

    @Override
    protected String primaryId() {
        return "id";
    }

    @Override
    protected String columns() {
        return " id AS id, name AS name,phone as phone";
    }

    @Override
    protected List<SqlPropertyValue<?>> propertyValue(User obj) {
        return null;
    }

    @Override
    public UserService test(JsonObject jsonObject, Handler<AsyncResult<JsonObject>> result) {
        DbUtils.getJdbcClient().getConnection(db->{
            if(db.succeeded()){
                selectAll(db.result(),rsp->{
                    if(rsp.succeeded()){
                        result.handle(Future.succeededFuture(ResultFormat.format(StatusCodeMsg.C200,rsp.result())));
                        //result.reply(ResultFormat.format(StatusCodeMsg.C200,rsp.result()));
                    }else{
                        result.handle(Future.failedFuture(ResultFormat.formatAsNewArray(StatusCodeMsg.C1500).toString()));
                    }
                });
            }else{
                result.handle(Future.failedFuture(db.cause().getMessage()));
                //handler.fail(500,"fail");
            }
        });
        return this;
    }
}
